package com.liushao.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liushao.entity.UserInfo;
import com.liushao.entity.vo.*;
import com.liushao.result.Result;
import com.liushao.service.UserInfoService;
import com.liushao.util.JwtUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author chen
 * @since 2025-01-03
 */
@RestController
@RequestMapping("/api/core/userInfo")
public class UserInfoController {

    // http://localhost/api/core/userInfo/register   post

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @ApiOperation("会员注册")
    @PostMapping("/register")
    public Result register(@RequestBody RegisterVO registerVO){

        String mobile = registerVO.getMobile();
        String password = registerVO.getPassword();
        if(StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
            return Result.error().message("账号,密码不能为空！");
        }
        String code = registerVO.getCode();

        //从redis中获取验证码
        String codeMsg = stringRedisTemplate.opsForValue().get("duanxin:" + mobile);
        if(!codeMsg.equals(code)){
            return Result.error().message("验证码有误!");
        }
        userInfoService.register(registerVO);

        return Result.ok().message("注册成功!");

    }

    // http://localhost/api/core/userInfo/login
    @ApiOperation("会员登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginVO loginVO, HttpServletRequest request ){
        String mobile = loginVO.getMobile();
        String password = loginVO.getPassword();
        if(StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
            return Result.error().message("账号或密码不能为空！");
        }
        //获取ip
        String ip = request.getRemoteAddr();
        UserInfoVO userInfoVO = userInfoService.login(loginVO,ip);
        return Result.ok().data("userInfo",userInfoVO);
    }


    // /api/core/userInfo/auth/getIndexUserInfo

    @ApiOperation("获取用户登录信息")
    @GetMapping("/auth/getIndexUserInfo")
    public Result getIndexUserInfo(HttpServletRequest request){
        //
        String token = null;
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies){
            if(cookie.getName().equals("token")){
                token = cookie.getValue();
                break;
            }
        }
        Long userId = JwtUtils.getUserId(token);
       UserIndexVO uiv= userInfoService.getUserIndexVoByUserId(userId);
        return Result.ok().data("userIndexVO",uiv);
    }

    @ApiOperation("校验令牌")
    @GetMapping("/checkToken")
    public Result checkToken(HttpServletRequest request){
        //String userInfo = request.getHeader("userInfo");
        //通过请求头获取 token 信息
        //
        String token = null;
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies){
            if(cookie.getName().equals("token")){
                token = cookie.getValue();
                break;
            }
        }
        //获取用户的 id
        boolean result = JwtUtils.checkToken(token);

        if(result){
              return  Result.ok();
        }else{
            return  Result.error();
        }

    }


  //  http://localhost/admin/core/userInfo/list/1/10
    @ApiOperation("获取会员分页列表")
    @GetMapping("/list/{page}/{limit}")
    public Result listPage(
            @ApiParam(value = "当前页码",required = true)
            @PathVariable Long page,
            @ApiParam(value = "每页记录数",required = true)
            @PathVariable Long limit,
            @ApiParam(value = "查询对象",required = false)
             UserInfoQuery userInfoQuery
            ){
          //mp中的分页类
        Page<UserInfo> pages = new Page<>(page,limit);

        IPage<UserInfo> infoIPage= userInfoService.listPage(pages,userInfoQuery);

        return Result.ok().data("pageModel",infoIPage);
    }

    /*
     .$get('/api/core/userInfo/auth/getIndexUserInfo')
        .then((response) => {
          this.userIndexVO = response.data.userIndexVO
     */


}

